import http from '@/utils/http'
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

export type Infos = {
  time: string[]
  [index: string]: unknown
}
type ChecksState = {
  applyList: Infos[]
  checkList: Infos[]
}
type GetApply = {
  applicantid?: string
  approverid?: string
}
type PostApply = {
  applicantid: string
  applicantname: string
  approverid: string
  approvername: string
  note: string
  reason: string
  time: [string, string]
}
type PutApply = {
  _id: string
  state: '已通过' | '未通过'
}

export const getApplyAction = createAsyncThunk(
  'checks/getApplyAction',
  async (payload: GetApply) => {
    const res = await http.get('/checks/apply', payload)
    return res.data
  }
)

export const postApplyAction = createAsyncThunk(
  'checks/postApplyAction',
  async (payload: PostApply) => {
    const res = await http.post('/checks/apply', payload)
    return res.data
  }
)

export const putApplyAction = createAsyncThunk(
  'checks/putApplyAction',
  async (payload: PutApply) => {
    const res = await http.put('/checks/apply', payload)
    return res.data
  }
)

const checkSlice = createSlice({
  name: 'checks',
  initialState: {
    applyList: [],
    checkList: [],
  } as ChecksState,
  reducers: {
    setApplyList: (state, action: PayloadAction<Infos[]>) => {
      state.applyList = action.payload
    },
    setCheckList: (state, action: PayloadAction<Infos[]>) => {
      state.checkList = action.payload
    },
  },
})

export const { setApplyList, setCheckList } = checkSlice.actions

export default checkSlice.reducer
